//
//  YTUserCommentViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/6/6.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTUserCommentViewModel.h"
#import "YTUserProfileServices.h"
#import "YTUserCommentModel.h"

@interface YTUserCommentViewModel ()

@end

@implementation YTUserCommentViewModel

- (void)initialize {
    self.dataSource = [NSMutableArray array];
    @weakify(self);
    self.requestRemoteDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            [self requestDataWithSubscriber:subscriber];
            return nil;
        }] initially:^{
            [self.dataSource removeAllObjects];
            self.page = 1;
        }];
    }];
    
    self.loadMoreDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [self requestDataWithSubscriber:subscriber];
            return nil;
        }] initially:^{
            self.page++;
        }];
    }];
}

- (void)requestDataWithSubscriber:(id<RACSubscriber>)subscriber {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"page"] = [NSString stringWithFormat:@"%zd", self.page];
    [[YTUserProfileServices userCommentForDriver:dict] subscribeNext:^(NSDictionary *response) {
        NSDictionary *data = response[@"responseData"];
        self.carStar = [NSString stringWithFormat:@"%@", data[@"carAverage"]];
        self.serviceStar = [NSString stringWithFormat:@"%@", data[@"serviceAverage"]];
        NSArray *dataArray = [NSArray yy_modelArrayWithClass:[YTUserCommentModel class] json:data[@"commentList"]];
        [self.dataSource addObjectsFromArray:dataArray];
        [subscriber sendNext:dataArray];
        [subscriber sendCompleted];
    }];
}

@end
